package com.bonc.modules.rpt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonc.common.utils.ObjectUtil;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.modules.item.param.ItemValueParam;
import com.bonc.modules.item.result.ItemChartDataResult;
import com.bonc.modules.item.utils.ChartDate;
import com.bonc.modules.item.utils.ChartDateUtils;
import com.bonc.modules.rpt.dao.ServUpgTsSumMonDao;
import com.bonc.modules.rpt.entity.ServUpgTsSumMon;
import com.bonc.modules.rpt.service.ServUpgTsSumMonServ;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@DataSource("gbasedata")
@Service
public class ServUpgTsSumMonServImpl extends ServiceImpl<ServUpgTsSumMonDao, ServUpgTsSumMon> implements ServUpgTsSumMonServ {

    private void appendDefaultCondition(LambdaQueryWrapper<ServUpgTsSumMon> queryWrapper, ItemValueParam servUpgTsSumMonParam){
        queryWrapper.eq(ServUpgTsSumMon::getClassDesc1,"汇总");
        queryWrapper.eq(ServUpgTsSumMon::getClassDesc2,"汇总");
        queryWrapper.eq(ServUpgTsSumMon::getClassDesc3,"汇总");
        queryWrapper.eq(ServUpgTsSumMon::getBusiType,"1");
    }

    private void appendRegionCondition(LambdaQueryWrapper<ServUpgTsSumMon> queryWrapper, ItemValueParam servUpgTsSumMonParam){

        if(!ObjectUtil.isEmptyObject(servUpgTsSumMonParam.getCityCode())){
            queryWrapper.eq(ServUpgTsSumMon::getRegionCode, servUpgTsSumMonParam.getCityCode());
        } else {
            queryWrapper.eq(ServUpgTsSumMon::getRegionCode, "00");
        }
        if(!ObjectUtil.isEmptyObject(servUpgTsSumMonParam.getCountyCode())){
            queryWrapper.eq(ServUpgTsSumMon::getAreaCode, servUpgTsSumMonParam.getCountyCode());
        } else {
            queryWrapper.eq(ServUpgTsSumMon::getAreaCode, "00");
        }
        if(!ObjectUtil.isEmptyObject(servUpgTsSumMonParam.getGridCode())){
            queryWrapper.eq(ServUpgTsSumMon::getGriddingCode, servUpgTsSumMonParam.getGridCode());
        } else {
            queryWrapper.eq(ServUpgTsSumMon::getGriddingCode, "00");
        }
    }

    @Override
    public ServUpgTsSumMon getUpgTextData(ItemValueParam servUpgTsSumMonParam){
        LambdaQueryWrapper<ServUpgTsSumMon> queryWrapper = new LambdaQueryWrapper<>();
        if(!ObjectUtil.isEmptyObject(servUpgTsSumMonParam.getDimId())){
            queryWrapper.eq(ServUpgTsSumMon::getDimId, servUpgTsSumMonParam.getDimId());
        }
        if(!ObjectUtil.isEmptyObject(servUpgTsSumMonParam.getDealDate())){
            queryWrapper.eq(ServUpgTsSumMon::getDealDate, servUpgTsSumMonParam.getDealDate().replaceAll("-", ""));
        }
        appendDefaultCondition(queryWrapper, servUpgTsSumMonParam);
        appendRegionCondition(queryWrapper, servUpgTsSumMonParam);
        List<ServUpgTsSumMon> servUpgTsSumMonList = baseMapper.selectList(queryWrapper);
        if(ObjectUtil.isEmptyObject(servUpgTsSumMonList)){
            return new ServUpgTsSumMon();
        } else {
            return servUpgTsSumMonList.get(0);
        }
    }

    private ItemChartDataResult lineChartConvert(List<ServUpgTsSumMon> servUpgTsSumMonList, List<Integer> dateList){

        List<String> xSeries = new ArrayList<>();
        List<Number> dataList = new ArrayList<>();
        if(!ObjectUtil.isEmptyObject(servUpgTsSumMonList)){
            Map<Integer, Number> dataMap = servUpgTsSumMonList.stream().collect(Collectors.toMap(servUpgTsSumMon -> servUpgTsSumMon.getDealDate(), e-> e.getUpgPrcnt() == null ? 0 : e.getUpgPrcnt()));
            for(int i = 0; i < dateList.size(); i++){
                xSeries.add(dateList.get(i).toString());
                dataList.add(dataMap.get(dateList.get(i)) == null ? 0 : dataMap.get(dateList.get(i)));
            }
        }
        return new ItemChartDataResult(xSeries, dataList);
    }

    @Override
    public ItemChartDataResult getUpgLineChart(ItemValueParam servUpgTsSumMonParam){
        LambdaQueryWrapper<ServUpgTsSumMon> queryWrapper = new LambdaQueryWrapper<>();
        if(!ObjectUtil.isEmptyObject(servUpgTsSumMonParam.getDimId())){
            queryWrapper.eq(ServUpgTsSumMon::getDimId, servUpgTsSumMonParam.getDimId());
        }
        appendDefaultCondition(queryWrapper, servUpgTsSumMonParam);
        appendRegionCondition(queryWrapper, servUpgTsSumMonParam);
        List<ChartDate> dateList = ChartDateUtils.getRecent(servUpgTsSumMonParam.getDealDate().toString(), servUpgTsSumMonParam.getDateType());
        List<Integer> dealDateList = dateList.stream().map(e -> Integer.valueOf(e.toString().replaceAll("-", ""))).collect(Collectors.toList());
        queryWrapper.in(ServUpgTsSumMon::getDealDate, dealDateList);
        return lineChartConvert(baseMapper.selectList(queryWrapper), dealDateList);
    }

    private ItemChartDataResult regionConvert(int regionType, List<ServUpgTsSumMon> servUpgTsSumMonList) {

        List<String> xAxis = new ArrayList<>();
        List<Number> dataList = new ArrayList<>();
        if(servUpgTsSumMonList != null) {
            dataList = servUpgTsSumMonList.stream().map(ServUpgTsSumMon::getUpgPrcnt).collect(Collectors.toList());
            switch (regionType) {
                case 1:
                    xAxis = servUpgTsSumMonList.stream().map(ServUpgTsSumMon::getAreaDesc).collect(Collectors.toList());
                    break;
                case 2:
                    xAxis = servUpgTsSumMonList.stream().map(ServUpgTsSumMon::getGriddingDesc).collect(Collectors.toList());
                    break;
                default:
                    xAxis = servUpgTsSumMonList.stream().map(ServUpgTsSumMon::getRegionDesc).collect(Collectors.toList());
                    break;
            }
        }
        return new ItemChartDataResult(xAxis, dataList);
    }


    @Override
    public ItemChartDataResult getRegionUpgChart(ItemValueParam  servUpgTsSumMonParam){
        int regionType = 0;
        LambdaQueryWrapper<ServUpgTsSumMon> queryWrapper = new LambdaQueryWrapper<>();
        if(!ObjectUtil.isEmptyObject(servUpgTsSumMonParam.getDimId())){
            queryWrapper.eq(ServUpgTsSumMon::getDimId, servUpgTsSumMonParam.getDimId());
        }

        // 日期
        if(!ObjectUtil.isEmptyObject(servUpgTsSumMonParam.getDealDate())){
            Integer dealDate = Integer.valueOf(servUpgTsSumMonParam.getDealDate().replaceAll("-", ""));
            queryWrapper.eq(ServUpgTsSumMon::getDealDate, dealDate);
        }

        // 区域
        if (cn.hutool.core.util.ObjectUtil.isNotEmpty(servUpgTsSumMonParam.getGridCode()) && !"00".equals(servUpgTsSumMonParam.getGridCode())) {
            // 同级网格
            regionType = 2;
            queryWrapper.eq(ServUpgTsSumMon::getRegionCode, servUpgTsSumMonParam.getCityCode());
            queryWrapper.eq(ServUpgTsSumMon::getAreaCode, servUpgTsSumMonParam.getCountyCode());
            queryWrapper.ne(ServUpgTsSumMon::getGriddingCode, "00");
        } else if (cn.hutool.core.util.ObjectUtil.isNotEmpty(servUpgTsSumMonParam.getCountyCode()) && !"00".equals(servUpgTsSumMonParam.getCountyCode())) {
            // 同级区县
            regionType = 1;
            queryWrapper.eq(ServUpgTsSumMon::getRegionCode, servUpgTsSumMonParam.getCityCode());
            queryWrapper.ne(ServUpgTsSumMon::getAreaCode, "00");
            queryWrapper.eq(ServUpgTsSumMon::getGriddingCode, "00");
        } else {
            // 同级地市
            regionType = 0;
            queryWrapper.notIn(ServUpgTsSumMon::getRegionCode, "00", "99");
            queryWrapper.eq(ServUpgTsSumMon::getAreaCode, "00");
            queryWrapper.eq(ServUpgTsSumMon::getGriddingCode, "00");
        }
        appendDefaultCondition(queryWrapper, servUpgTsSumMonParam);
        queryWrapper.orderByAsc(ServUpgTsSumMon::getRegionCode, ServUpgTsSumMon::getAreaCode, ServUpgTsSumMon::getGriddingCode);
        return regionConvert(regionType, baseMapper.selectList(queryWrapper));
    }
}
